package com.lyy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyy.handler.BusinessException;
import com.lyy.response.ResultCode;
import com.lyy.system.entity.Product;
import com.lyy.system.entity.ProductCategory;
import com.lyy.system.mapper.ProductMapper;
import com.lyy.system.service.ProductService;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Override
    public void addProduct(Product product) {
        SimpleDateFormat format =new SimpleDateFormat("YYYYMMdd");
        Date date=new Date();
        //六位随机数
        int random=(int) (Math.random()*100000);
        //商品编号格式；PN+年月日+6位随机数
        String pnum="PN"+format.format(date)+random;
        product.setPNum(pnum);
        //未审核
        product.setStatus(2);
        QueryWrapper<Product> wrapper= new QueryWrapper<>();
        wrapper.orderByDesc("create_time").last(" limit 1");
        Product product1=this.baseMapper.selectOne(wrapper);
        //排序
        if (product1 == null){
            product.setSort(1);
        }else {
            product.setSort(product1.getSort()+1);
        }
        this.baseMapper.insert(product);
    }

    @Override
    public IPage<Product> queryProuctList(Page<Product> page, QueryWrapper<Product> wrapper) {
        return this.baseMapper.queryProuctList(page,wrapper);
    }

    @Override
    public void deleteProduct(Long id) {
        Product product =this.baseMapper.selectById(id);
       // 只有物资处于回收站,或者待审核的情况下可删除
        if (product.getStatus()!=0 && product.getStatus()!=2){
            throw new BusinessException(ResultCode.PRODUCT_DELETE.getCode(),
                    ResultCode.PRODUCT_DELETE.getMessage());
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public Product productDetail(Long id) {
        Product product =this.baseMapper.selectById(id);
        return product;
    }

    @Override
    public void updateProduct(Product product) {
        this.baseMapper.updateById(product);
    }

    @Override
    public void remove(Long id) {
        Product product=new Product();
        product.setStatus(0);
        product.setModifiedTime(new Date());
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        this.baseMapper.update(product,wrapper);
    }

    @Override
    public void back(Long id) {
        Product product =new Product();
        product.setStatus(1);
        product.setModifiedTime(new Date());
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        this.baseMapper.update(product,wrapper);
    }

    @Override
    public void publish(Long id) {
        Product product=new Product();
        product.setStatus(1);
        product.setModifiedTime(new Date());
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        this.baseMapper.update(product,wrapper);
    }

}
